﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;
using EventHub.Data.DataModel;

namespace EventHub.UI.ViewModel
{
    public class EventTreeViewModel
    {
        readonly ReadOnlyCollection<SACBusinessEventViewModel> _rootGeneration;
        readonly SACBusinessEventViewModel _rootEvent;
        readonly ICommand _searchCommand;

        string _contextEvent = String.Empty;

        public EventTreeViewModel(SACBusinessEvent rootEvent)
        {           
            _rootEvent = new SACBusinessEventViewModel(rootEvent);            

            _rootGeneration = new ReadOnlyCollection<SACBusinessEventViewModel>(
                new SACBusinessEventViewModel[]
                {
                    _rootEvent 
                });

            _searchCommand = new SearchEventTreeCommand(this);
        }

        #region SearchCommand

        /// <summary>
        /// Returns the command used to execute a search in the family tree.
        /// </summary>
        public ICommand SearchCommand
        {
            get { return _searchCommand; }
        }

        private class SearchEventTreeCommand : ICommand
        {
            readonly EventTreeViewModel _eventTree;

            public SearchEventTreeCommand(EventTreeViewModel eventTree)
            {
                _eventTree = eventTree;
            }

            public bool CanExecute(object parameter)
            {
                return true;
            }

            event EventHandler ICommand.CanExecuteChanged
            {
                // I intentionally left these empty because
                // this command never raises the event, and
                // not using the WeakEvent pattern here can
                // cause memory leaks.  WeakEvent pattern is
                // not simple to implement, so why bother.
                add { }
                remove { }
            }

            public void Execute(object parameter)
            {
                _eventTree.PerformSearch();
            }
        }

        #endregion // SearchCommand

        public ReadOnlyCollection<SACBusinessEventViewModel> RootGeneration
        {
            get { return _rootGeneration; }
        } 

        public string ContextEvent
        {
            get
            {
                return _contextEvent;
            }
            set
            {
                if (value == _contextEvent)
                    return;

                _contextEvent = value;
            }
        }

        void PerformSearch()
        {
            PerformSearch(this.RootGeneration.First());
        }

        void PerformSearch(SACBusinessEventViewModel eventViewModel)
        {
            if (eventViewModel.Equals(_contextEvent))
            {
                eventViewModel.IsSelected = true;
                eventViewModel.IsExpanded = true;
                return;
            }
            else
            {
                foreach (var childEvent in eventViewModel.ChildEvents)
                {
                    PerformSearch(childEvent);
                }
            }
        }
    }
}
